package com.zhongke.common.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhongke.common.R
import com.zhongke.common.base.viewmodel.ZKBaseViewModel
import com.zhongke.common.databinding.CommonFragmentBaseListBinding
import com.zhongke.common.utils.ZKToastUtils
import com.zhongke.common.widget.rlv.CommonRecyclerView
import com.zhongke.common.widget.rlv.adapter.BaseQuickAdapter
import com.zhongke.core.http.httpbase.exception.AppException
import com.zhongke.common.utils.gone
import com.zhongke.common.utils.isVisible
import com.zhongke.common.utils.visible

/**
 * @author: ZhangQingHua
 * @date: 2021/11/25
 * @description 只有一个列表的 Fragment
 * 
 */
abstract class ZKBaseListFragment<T, V : ZKBaseViewModel<*>> : ZKBaseFragment<CommonFragmentBaseListBinding, V>() {

    companion object {
        const val PAGE_SIZE = 30
    }

    private var isLoadMore = false

    final override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?) =
        R.layout.common_fragment_base_list

    abstract fun getAdapter(): BaseQuickAdapter<T, *>
    abstract fun doGetData(pageNo: Int = 1)
    protected open fun isGetDataOnFirstVisible() = true

    override fun onFragmentFirstVisible() {
        if (isGetDataOnFirstVisible()) {
            getData()
        }
    }

    @CallSuper
    override fun initData() {
        initList()
        initRemainViews()
        customList(binding.list)
    }

    private fun initRemainViews() {
        binding.statusView.setReLoadClickListener {
            getData()
        }
    }

    private fun initList() {
        val adapter = getAdapter()
        binding.list.apply {
            setLayoutManager(LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false))
            setAdapter(getAdapter())
            setiCommonRecyclerView(false) {
                getData(it)
            }
            adapter.setOnItemClickListener { _, _, position ->
                onItemClick(adapter.getItem(position))
                onItemClick(adapter.getItem(position),position)
            }
            adapter.setOnItemLongClickListener { _, _, position ->
                onItemLongClick(adapter.getItem(position))
                onItemLongClick(adapter.getItem(position),position)
            }
        }
    }

    fun getData(pageNo: Int = 1) {
        isLoadMore = pageNo != 1
        if (binding.statusView.isVisible()) {
            binding.statusView.showLoading()
        }
        doGetData(pageNo)
    }

    protected open fun customList(commonRecyclerView: CommonRecyclerView) {
    }

    /**
     * 刷新数据
     */
    protected fun refreshData() {
        binding.list.setAutoRefresh()
    }

    protected open fun onBeforeUpdateData() {
    }

    protected fun onGetDataSuccess(data: List<T>) {
        onBeforeUpdateData()

        binding.apply {
            statusView.gone()
            list.visible()
            list.setListData(data)
            if (!isLoadMore) {
                list.rlvData.scrollToPosition(0)
            }
        }

        onAfterUpdateData()
    }

    protected open fun onAfterUpdateData() {
    }

    protected open fun onItemClick(item: T) {}
    protected open fun onItemClick(item: T,pos: Int) {}
    protected open fun onItemLongClick(item: T) = true
    protected open fun onItemLongClick(item: T,pos: Int) = true

    protected fun onGetDataFailed(ex: AppException) {
        binding.apply {
            statusView.apply {
                if (isVisible()) {
                    showRetry(ex.errorMsg)
                } else {
                    list.setAppException(ex)
                    ZKToastUtils.showShort(ex.errorMsg)
                }
            }
        }
    }

    protected open fun setNoDataText(text: String) =
        binding.list.setNoDataText(text)

    protected open fun setFooterText(text: String) =
        binding.list.setFooterText(text)
}